import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:people_big_people/base/base_scaffold.dart';
import 'package:people_big_people/base/base_state.dart';
import 'package:people_big_people/bean/testReq/juhe.dart';
import 'package:people_big_people/net/api_post.dart';
import 'package:people_big_people/page/main_bottom_tab_page/home/vm/home_vm.dart';
import 'package:people_big_people/utils/log_util.dart';
import 'package:preload_page_view/preload_page_view.dart';
import 'package:provider/provider.dart';
import 'package:video_player/video_player.dart';

import '../../event/home_refresh_event.dart';
import '../../net/dio_manager.dart';
import '../../style/physics.dart';
import '../../test/test_data.dart';
import '../../utils/event_bus_utils.dart';
import '../../utils/screen.dart';
import '../../video/mock/video_url_bean.dart';
import '../../video/video_page.dart';
import '../../view/refresh_list_view.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var arguments = ModalRoute.of(context)?.settings.arguments;
    return ChangeNotifierProvider(
      create: (context) => HomeVM(),
      child: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends BaseState<HomePage> with AutomaticKeepAliveClientMixin {
  late PreloadPageController _preloadPageController;

  ///缓存数量
  final int _preloadPagesCount = 1;
  int _currentIndex = 0;

  @override
  void initState() {
    _preloadPageController = PreloadPageController();
    _getRefreshHome();
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {});
  }

  ///订阅home刷新消息
  void _getRefreshHome() {
    eventBus.on<HomeRefreshEvent>((event) {
      event.refresh;
      LogUtil.i("message      ${event.refresh}");
      Juhe juhe = Juhe();
      juhe.data = "9/1";
      juhe.key = "0250c4c75504cac82571a62b7908acbb";
      var api = apiPost.queryEvent(juhe);
      api.then((value) => {LogUtil.i("api=============>${value}")});
    });
    updateState(() { });
  }

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
        body: Container(
      child: pageWidget(),
    ));
  }



  ///刷线列表
  Widget refreshWidget() {
    return GoodsRefreshListView(children: [
      VideoPage(
        currentIndex: _currentIndex,
        index: 0,
      )
    ]);
  }

  ///切换页面
  Widget pageWidget() => PageView.builder(
        physics: const QuickerScrollPhysics(),
        scrollDirection: Axis.vertical,
        itemCount: 4,
        itemBuilder: (context, index) {
          return VideoPage(
            currentIndex: _currentIndex,
            index: index,
          );
        },
        onPageChanged: (index) {
          _currentIndex = index;
          LogUtil.i("_currentIndex    $_currentIndex");
        },
      );

  ///待缓存的切换组件
  Widget preloadWidget() => PreloadPageView.builder(
        physics: const QuickerScrollPhysics(),
        scrollDirection: Axis.vertical,
        //缓存数量
        preloadPagesCount: _preloadPagesCount,
        controller: _preloadPageController,
        itemCount: 4,
        itemBuilder: (context, index) {
          return VideoPage(
            currentIndex: _currentIndex,
            index: index,
          );
        },
        onPageChanged: (index) {
          _currentIndex = index;
          setState(() {});
        },
      );

  @override
  bool get wantKeepAlive => false;
}
